package com.dynasty;

/**
 * @description 单例模式
 * Created by wangchao12 on 2019/4/25 9:21.
 */

public class Singleton {
    /** 加volatile这的原因是防止指令重排序，避免多线程环境下出现问题 **/
    private static volatile Singleton singleton = null;

    private Singleton() {
    }

    /**
     * 方法的功能描述：返回单实例-双重检验锁
     *
     * @return
     */
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
